package com.gome.ocean.controller.authority;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.gome.ocean.common.utils.ResponsePage;
import com.gome.ocean.dao.model.authority.GSystemUpdateLog;
import com.gome.ocean.service.authority.SystemUpdateLogService;
import com.gome.ocean.service.base.AuthorityBaseController;
import com.google.common.collect.Maps;

/**
 * @author liyuefeng
 */
@Controller
@RequestMapping(value = "/syslog")
public class SystemUpdateLogController extends AuthorityBaseController {

    @Autowired
    private SystemUpdateLogService systemUpdateLogService;

    @RequestMapping(value = "/toRunSystemUpdatePage", method = RequestMethod.GET)
    public ModelAndView toRunSystemUpdatePage(ModelAndView mav, HttpServletRequest request) {

        mav.setViewName("/authority/systemupdatelog/menu");
        return mav;
    }

    @RequestMapping(value = "/saveSystemUpdateLog", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage saveSystemUpdateLog(ResponsePage page, HttpServletRequest request) {
        String inputer = this.getCurrentUser();
        String addtime = request.getParameter("addtime");
        String loglist = request.getParameter("loglist");
        String status = request.getParameter("status");
        if ("1".equals(status)) {
            upNow(inputer, addtime, loglist);
            page.setSuccess(true);
        } else {
            upSoon(inputer, addtime, loglist);
            page.setSuccess(true);
        }

        return page;
    }

    private void upNow(String inputer, String addtime, String loglist) {
        JSONArray logJsonArray = JSONArray.parseArray(loglist);
        systemUpdateLogService.updateAll();
        for (Object object : logJsonArray) {
            JSONObject logJson = JSONObject.parseObject(object.toString());
            GSystemUpdateLog gSystemUpdateLog = new GSystemUpdateLog();
            gSystemUpdateLog.setLogContent(logJson.getString("context"));
            gSystemUpdateLog.setLogScope(logJson.getByte("type"));
            gSystemUpdateLog.setLogStatus((byte) 2);
            gSystemUpdateLog.setLogTitle(logJson.getString("title"));
            gSystemUpdateLog.setUpdateInputer(inputer);
            gSystemUpdateLog.setAddTimeNode(addtime);
            systemUpdateLogService.insertSelective(gSystemUpdateLog);
        }
    }

    private void upSoon(String inputer, String addtime, String loglist) {
        JSONArray logJsonArray = JSONArray.parseArray(loglist);
        for (Object object : logJsonArray) {
            JSONObject logJson = JSONObject.parseObject(object.toString());
            GSystemUpdateLog gSystemUpdateLog = new GSystemUpdateLog();
            gSystemUpdateLog.setLogContent(logJson.getString("context"));
            gSystemUpdateLog.setLogScope(logJson.getByte("type"));
            gSystemUpdateLog.setLogStatus((byte) 2);
            gSystemUpdateLog.setLogTitle(logJson.getString("title"));
            gSystemUpdateLog.setUpdateInputer(inputer);
            gSystemUpdateLog.setAddTimeNode(addtime);
            systemUpdateLogService.insertSelective(gSystemUpdateLog);
        }
    }

    @RequestMapping(value = "/getLogList", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage getLogList(ResponsePage page, HttpServletRequest request) {
        int pageNum = getPageNum();
        int pageSize = getPageSize();
        Page<GSystemUpdateLog> logsGroup = systemUpdateLogService.getList(pageNum, pageSize);
        if (logsGroup.size() > 0) {
            page.setPage(logsGroup, FIND_SUCCESS);
        }
        return page;
    }

    @RequestMapping(value = "/getlog", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage getlog(ResponsePage page, HttpServletRequest request, Long id) {

        GSystemUpdateLog gSystemUpdateLog = systemUpdateLogService.selectByPrimaryKey(id);
        if (gSystemUpdateLog == null) {
            page.setErrorMsg(FIND_NO_RESULTS);
        } else {
            page.setSingleObject(gSystemUpdateLog, FIND_NO_RESULTS);
        }
        return page;
    }

    @RequestMapping(value = "/updata", method = RequestMethod.POST)
    @ResponseBody
    public ResponsePage update(ResponsePage page, HttpServletRequest request, GSystemUpdateLog gSystemUpdateLog) {
        boolean flag = systemUpdateLogService.updateByPrimaryKeySelective(gSystemUpdateLog);
        if (flag) {
            page.setSuccessMsg(UPDATE_SUCCESS);
        } else {
            page.setErrorMsg(UPDATE_FAILURE);
        }
        return page;
    }

    @RequestMapping("/showVersionHistory")
    public ModelAndView showVersionHistory(ModelAndView mav) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("logStatus", 2);
        Map<String, Object> resultMap = systemUpdateLogService.getAllSysLogByMap(map);
        mav.addObject("resultMap", resultMap);
        mav.setViewName("optlog/ocean-version-history");
        return mav;
    }

    @RequestMapping("/deleteLogs")
    @ResponseBody
    public ResponsePage deleteLogs(HttpServletRequest request) {
        ResponsePage page = new ResponsePage();
        Long id = Long.parseLong(request.getParameter("id"));
        boolean flag = systemUpdateLogService.deleteByPrimaryKey(id);
        if (flag) {
            return page.setSuccessMsg("删除成功");
        }
        return page.setErrorMsg("删除失败");
    }
}
